{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 产品类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入 ABCMeta 和 abstractmethod来定义抽象类和抽象方法\n",
    "from abc import ABCMeta, abstractmethod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FruitJuice(metaclass=ABCMeta):\n",
    "    \"\"\"果汁\"\"\"\n",
    "\n",
    "    def __init__(self, name):\n",
    "        self.__name = name\n",
    "\n",
    "    def get_name(self):\n",
    "        return self.__name\n",
    "\n",
    "    @abstractmethod\n",
    "    def get_juice(self):\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class AppleJuice(FruitJuice):\n",
    "    \"\"\"苹果汁\"\"\"\n",
    "\n",
    "    def __init_(self, name):\n",
    "        super().__init__(name)\n",
    "\n",
    "    def get_juice(self):\n",
    "        return \"苹果汁\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class OrangeJuice(FruitJuice):\n",
    "    \"\"\"橙汁\"\"\"\n",
    "\n",
    "    def __init_(self, name):\n",
    "        super().__init__(name)\n",
    "\n",
    "    def get_juice(self):\n",
    "        return \"橙汁\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 工厂类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Juicer:\n",
    "    \"\"\"榨汁机\"\"\"\n",
    "\n",
    "    @staticmethod\n",
    "    def make_juice(fruit):\n",
    "        if fruit == \"苹果\":\n",
    "            fruit_juice = AppleJuice(fruit)\n",
    "        elif fruit == \"橙子\":       \n",
    "            fruit_juice = OrangeJuice(fruit)\n",
    "        else:\n",
    "            raise ValueError(\"不支持的参数: %s\" % fruit)\n",
    "        return fruit_juice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'苹果汁'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apple_juice = Juicer.make_juice(\"苹果\")\n",
    "apple_juice.get_juice()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'橙汁'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orange_juice = Juicer.make_juice(\"橙子\")\n",
    "orange_juice.get_juice()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8c3cb1439b48e6ef114929de33211eafda20be62493369cd313c9c2b7c59c15d"
  },
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
